package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.dto.DIshDto;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    /**
     * 添加菜品，既要插入菜品表，也要插入菜品口味表
     * @param dIshDto
     */
    @Autowired
    private DishFlavorService dishFlavorService;
    //这里涉及到两张表的新增，所以要开启事务
    @Override
    @Transactional//记得在启动类中开启事务管理，默认是启动的
    public boolean saveDishAndDish_favor(DIshDto dIshDto) {

        //首先将基本信息插入dish表，因为DishDto继承dish，所以可以以dishDto为参数进行插入
        boolean save = this.save(dIshDto);

        //将flavors信息插入到dish_flavor表中
        //注意，插入数据到dish_flavor表，处理favor中的name和flavor之外，还需要dish_id这个参数
        //因为我们要知道这是哪个菜品的口味

        //获取dish_id
        //因为上面已经执行了dish的新增操作，所以此时dishDto里面是包含了dishId
        Long id = dIshDto.getId();
        //给dishDto中的flavor中的DishFlavor对象中的dish_id赋值
        List<DishFlavor> flavors = dIshDto.getFlavors();
        //使用steam()方式遍历flavors,给其中的dish_id赋值，然后重新赋值给favors
        flavors=flavors.stream().map((item)->{
           item.setDishId(id);
           return item;
        }).collect(Collectors.toList());

        //将flavor中的数据插入搭配DishFlavor表中
        boolean batch = dishFlavorService.saveBatch(dIshDto.getFlavors());

        if (save&batch){
            return true;
        }else{
            return false;
        }

    }

    /**
     * 根据id查找dish表和dish_flavor表的数据，进行回显操作
     * @param id
     * @return
     */
    @Override
    public DIshDto selectDishAndDish_flavor(Long id) {
        //查询dish表
        Dish dish = this.getById(id);
        //将dish拷贝到dishDto对象中
        DIshDto dIshDto=new DIshDto();
        //因为disDto是继承dish的，所以可以直接
        BeanUtils.copyProperties(dish,dIshDto);
        //查询dish_flavor表，拿到flavor
        LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(id!=null,DishFlavor::getDishId,id);
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        //将dishFlavor的flavor拿出来，赋值给dishDto里面
        dIshDto.setFlavors(list);
        return dIshDto;
    }

    /**
     *
     * @param dIshDto
     * @return
     */
    @Override
    public boolean updateDishAndDish_flavor(DIshDto dIshDto) {
        //更新dish表中的数据
        boolean b = this.updateById(dIshDto);
        //修改dish_flavor表中的口味数据
        //删除dish_flavor表中的相关口味数据
        Long id = dIshDto.getId();
        LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,id);
        dishFlavorService.remove(queryWrapper);
        //然后插入口味数据
        List<DishFlavor> flavors = dIshDto.getFlavors();
        flavors=flavors.stream().map((item)->{
            item.setDishId(id);
            return item;
        }).collect(Collectors.toList());
        boolean b1 = dishFlavorService.saveBatch(flavors);
        if (b&b1){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 根据套餐菜品种类查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> getByCategoryId(Long categoryId) {
        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(categoryId!=null,Dish::getCategoryId,categoryId);
        List<Dish> list = this.list(queryWrapper);
        return list;
    }
}
